home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / pyxmpp / resolver.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  3KB  |  116 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. __revision__ = '$Id: resolver.py 503 2005-01-03 21:59:36Z jajcus $'
  5. __docformat__ = 'restructuredtext en'
  6. import re
  7. import socket
  8. import dns.resolver as dns
  9. import dns.name as dns
  10. import dns.exception as dns
  11. import random
  12. from encodings import idna
  13. service_aliases = {
  14.     'xmpp-server': ('jabber-server', 'jabber') }
  15. ip_re = re.compile('\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}')
  16.  
  17. def shuffle_srv(records):
  18.     if not records:
  19.         return []
  20.     
  21.     ret = []
  22.     while len(records) > 1:
  23.         weight_sum = 0
  24.         for rr in records:
  25.             weight_sum += rr.weight + 0.1
  26.         
  27.         thres = random.random() * weight_sum
  28.         weight_sum = 0
  29.         for rr in records:
  30.             weight_sum += rr.weight + 0.1
  31.             if thres < weight_sum:
  32.                 records.remove(rr)
  33.                 ret.append(rr)
  34.                 break
  35.                 continue
  36.         
  37.     ret.append(records[0])
  38.     return ret
  39.  
  40.  
  41. def reorder_srv(records):
  42.     records = list(records)
  43.     records.sort()
  44.     ret = []
  45.     tmp = []
  46.     for rr in records:
  47.         if not tmp or rr.priority == tmp[0].priority:
  48.             tmp.append(rr)
  49.             continue
  50.         
  51.         ret += shuffle_srv(tmp)
  52.     
  53.     if tmp:
  54.         ret += shuffle_srv(tmp)
  55.     
  56.     return ret
  57.  
  58.  
  59. def resolve_srv(domain, service, proto = 'tcp'):
  60.     names_to_try = [
  61.         u'_%s._%s.%s' % (service, proto, domain)]
  62.     if service_aliases.has_key(service):
  63.         for a in service_aliases[service]:
  64.             names_to_try.append(u'_%s._%s.%s' % (a, proto, domain))
  65.         
  66.     
  67.     for name in names_to_try:
  68.         name = idna.ToASCII(name)
  69.         
  70.         try:
  71.             r = dns.resolver.query(name, 'SRV')
  72.         except dns.exception.DNSException:
  73.             continue
  74.  
  75.         if not r:
  76.             continue
  77.         
  78.         return [ (rr.target.to_text(), rr.port) for rr in reorder_srv(r) ]
  79.     
  80.  
  81.  
  82. def getaddrinfo(host, port, family = 0, socktype = socket.SOCK_STREAM, proto = 0, allow_cname = True):
  83.     ret = []
  84.     if proto == 0:
  85.         proto = socket.getprotobyname('tcp')
  86.     elif type(proto) != int:
  87.         proto = socket.getprotobyname(proto)
  88.     
  89.     if type(port) != int:
  90.         port = socket.getservbyname(port, proto)
  91.     
  92.     if family not in (0, socket.AF_INET):
  93.         raise NotImplementedError, 'Protocol family other than AF_INET not supported, yet'
  94.     
  95.     if ip_re.match(host):
  96.         return [
  97.             (socket.AF_INET, socktype, proto, host, (host, port))]
  98.     
  99.     host = idna.ToASCII(host)
  100.     
  101.     try:
  102.         r = dns.resolver.query(host, 'A')
  103.     except dns.exception.DNSException:
  104.         r = dns.resolver.query(host + '.', 'A')
  105.  
  106.     if not allow_cname and r.rrset.name != dns.name.from_text(host):
  107.         raise ValueError, 'Unexpected CNAME record found for %s' % (host,)
  108.     
  109.     if r:
  110.         for rr in r:
  111.             ret.append((socket.AF_INET, socktype, proto, r.rrset.name, (rr.to_text(), port)))
  112.         
  113.     
  114.     return ret
  115.  
  116.